package com.xpgk.xpsoft.run;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.controller.BaseController;
import com.xpgk.common.kit.CacheKitEx;
import com.xpgk.xpsoft.constants.XPSoftConstant;

public class RunStateController extends BaseController {
	public void index() {
		keepPara();
		if (CacheKitEx.IsUseRedis())
			CacheKitEx.put(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_Handle_RunState_Enable, 1);
		;
		if (CacheKitEx.IsUseRedis()) {
			List<Record> list = new ArrayList<Record>();
			long num, num5, useTime;
			num = XPSoftConstant.RunState_Num;
			num5 = XPSoftConstant.RunState_Num50;
			useTime = XPSoftConstant.RunState_UseTime;

			Record state = new Record();
			state.set("name", "处理次数").set("value", num).set("bz", "每5秒内，系统处理的任务数");
			list.add(state);
			state = new Record();
			state.set("name", "处理时间").set("value", useTime / 1000000).set("bz", "每5秒内，系统处理的任务数所用的时间和");
			list.add(state);
			state = new Record();
			state.set("name", "平均处理时间").set("value", (double) (useTime / 100000 / (num == 0 ? 1 : num)) / 10).set("bz",
					"每5秒内，系统处理的任务所用的平均时间");
			list.add(state);
			state = new Record();
			state.set("name", ">50毫秒次数").set("value", num5).set("bz", "每5秒内，系统处理的任务所用的时间大于50毫秒的次数");
			list.add(state);
			setAttr("lists", list);
		}
		String t = getPara("date");
		List<Record> state;
		if (DbKit.getConfig().getDialect().isOracle())
			state = Db.find(Db.getSqlPara("xpSoftRunState.findListOracle", Kv.by("gxsj", t)));
		else
			state = Db.find(Db.getSqlPara("xpSoftRunState.findList", Kv.by("gxsj", t)));
		long num[] = new long[state.size()];
		long num50[] = new long[state.size()];
		long sumTime[] = new long[state.size()];
		long sumTimePer[] = new long[state.size()];
		String labels[] = new String[state.size()];
		int i = 0;
		for (Iterator<Record> iterator = state.iterator(); iterator.hasNext();) {
			Record record = (Record) iterator.next();
			num[i] = record.getInt("num");
			num50[i] = record.getInt("num50");
			sumTime[i] = record.getLong("sumTime") / 1000000;
			sumTimePer[i] = sumTime[i] / (num[i] == 0 ? 1 : num[i]);
			labels[i] = record.get("gxsj").toString();
			i++;
		}
		setAttr("num", Arrays.toString(num));
		setAttr("num50", Arrays.toString(num50));
		setAttr("sumTime", Arrays.toString(sumTime));
		setAttr("sumTimePer", Arrays.toString(sumTimePer));
		setAttr("labels", Arrays.toString(labels));
		render("index.html");
	}

	public void pressTest() {
		render("pressTest.html");
	}
}
